package android.moodle;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

import static android.moodle.R.*;

/**
 * Created by IntelliJ IDEA.
 * User: michelle
 * Date: 4/10/11
 * Time: 2:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class MenuActivity extends Activity {
    private String[] items;

    private AlertDialog alert;
    private AlertDialog alert2;



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.menu);

        typeOfGameDialog(this);
        whatToDoDialog(this);



        try {
            addMenuItems();
        } catch (Exception e) {
           Log.e("changeToHelpScreen", e.toString());
        }


    }

    public void addMenuItems() {

        ListView lv = (ListView)findViewById(id.menuList);
        items = new String[] {getResources().getString(string.multiplayer),
                getResources().getString(string.scores),getResources().getString(string.help),
                getResources().getString(string.quit)};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, layout.menu_item, items);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id){

                TextView tv = (TextView)itemClicked;
                String text = tv.getText().toString();
                try {
                    if(text.equals(items[0])) {
                        alert.show();
                    } else if(text.equals(items[1])) {
                        changeToScoresScreen();
                    } else if(text.equals(items[2])) {
                        changeToHelpScreen();
                    } else if(text.equals(items[3])) {
                         finish();
                    }
                } catch (Exception e) {
                    Log.e("Failure", e.toString());
                }
            }
        });

    }


    private void changeToHelpScreen()  {
        startActivity(new Intent(this, HelpActivity.class));
    }

    private void changeToScoresScreen() {
        startActivity(new Intent(this, ScoresActivity.class));

    }

    private void newGameOnPhone() {
        startActivity(new Intent(this, NewGameOnPhoneActivity.class));
    }

    private void joinGame() {
        startActivity(new Intent(this, JoinGameActivity.class));
    }

    private void createGame() {
        startActivity(new Intent(this, CreateGameActivity.class));
    }

    /*public void startGame(View v) {
        String player1 = pl1.getText().toString().trim();
        String player2 = pl1.getText().toString().trim();
        gamePrefs = getSharedPreferences(PLAYER_NAMES,0);
        SharedPreferences.Editor editor = gamePrefs.edit();
        editor.putString(PLAYER1, player1);
        editor.putString(PLAYER2, player2);
        editor.commit();

        Intent i = new Intent(this, GameActivity.class);
        startActivity(i);
    }

    public void goBack(View v) {
        this.setContentView(layout.menu);
        addMenuItems();
    } */


    private void typeOfGameDialog(Context c) {
        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setMessage(R.string.typeOfGame);

        builder.setPositiveButton(c.getResources().getText(R.string.network), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                alert2.show();
            }
        });
        builder.setNeutralButton(c.getResources().getText(R.string.phone), new DialogInterface.OnClickListener() {
            @Override
            public  void onClick(DialogInterface dialogInterface, int i) {
                newGameOnPhone();

            }

        });
        alert = builder.create();
    }

    private void whatToDoDialog(Context c) {
        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setMessage(R.string.doOnNetwork);

        builder.setNeutralButton(getResources().getText(R.string.joinGame), new DialogInterface.OnClickListener() {
           @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               joinGame();

           }
        });

        builder.setPositiveButton(getResources().getText(R.string.createGame), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                createGame();

            }
        });
        alert2 = builder.create();
    }




}